package jgl.vajra.kingkong.widget.manager;

import jgl.vajra.kingkong.widget.bean.BannerInfo;
import jgl.vajra.kingkong.widget.loopviewpage.AutoLoopSwitchBaseView;
import jgl.vajra.kingkong.widget.loopviewpage.AutoSwitchAdapter;
import jgl.vajra.kingkong.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.kingkong.widget.retrofithelper.rxsubscriber.CommonObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import io.reactivex.Observable;
import io.reactivex.Observer;

/**
 * Created by Mloong on 2018/8/3.
 */

public class BannerManager {
    private static class BannerManagerSingleton {
        private static final BannerManager INSTANCE = new BannerManager();

    }

    public static BannerManager getInstance() {
        return BannerManager.BannerManagerSingleton.INSTANCE;
    }


    public void setBannerDataWithType(final AppCompatActivity act, Map<String, Object> map, final AutoLoopSwitchBaseView viewPager) {
        if (map == null) {
            map = new HashMap<>();
        }
        requestData(act, map, viewPager, null);
    }


    public void setBannerDataWithType(final AppCompatActivity act, int type, final AutoLoopSwitchBaseView viewPager) {

        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        requestData(act, map, viewPager, null);
    }

    public void setBannerDataWithType(final AppCompatActivity act, int type, Observer<List<BannerInfo>> observer) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        requestData(act, map, null, observer);
    }

    public void setBannerDataWithType(final AppCompatActivity act, Map<String, Object> map, Observer<List<BannerInfo>> observer) {
        if (map == null) {
            map = new HashMap<>();
        }
        requestData(act, map, null, observer);
    }

    private void requestData(final AppCompatActivity act, Map<String, Object> map, final AutoLoopSwitchBaseView viewPager, Observer<List<BannerInfo>> observer) {
        Observable<List<BannerInfo>> compose = RetrofitJsonManager.getInstance().getApiService().getBannerInfo(map).compose(RxJavaHttpManager.<List<BannerInfo>>applyTransformer());
        if (observer != null) {
            compose.subscribe(observer);
            return;
        }
        compose.subscribe(new CommonObserver<List<BannerInfo>>() {
            @Override
            public void onNext(List<BannerInfo> bannerInfos) {
                super.onNext(bannerInfos);
                setBannerView(act, viewPager, bannerInfos);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                List<BannerInfo> infoList = new ArrayList<>();
                BannerInfo info = new BannerInfo();
                info.coverImage = "";
                infoList.add(info);
                setBannerView(act, viewPager, infoList);
            }
        });
    }

    public void setBannerView(FragmentActivity act, AutoLoopSwitchBaseView viewPager, List<BannerInfo> bannerInfos) {
        AutoSwitchAdapter mAdapter = new AutoSwitchAdapter(act, bannerInfos);
        viewPager.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }

    public void setBannerViewStringData(AppCompatActivity act, AutoLoopSwitchBaseView viewPager, List<BannerInfo> bannerInfos) {
        AutoSwitchAdapter mAdapter = new AutoSwitchAdapter(act, bannerInfos);
        viewPager.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }
}
